package com.xmutca.sso.dao;

import com.xmutca.core.mybatis.MyBatisDao;
import com.xmutca.core.mybatis.lang.MybatisExtendedLanguageDriver;
import com.xmutca.sso.api.Permission;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author: 彭伟煌(pengweihuang@xmutca.com)
 * @create: 2016-08-29 15:53
 */
@MyBatisDao
public interface UserPermissionDao {

    /**
     * 用户权限新增
     * @param user
     * @param permission
     * @return
     */
    @Insert("insert into t_sys_user_permission(user, permission) values(#{user}, #{permission})")
    int add(@Param("user") String user, @Param("permission") Integer permission);

    /**
     * 角色权限删除
     * @param user
     * @param permission
     * @return
     */
    @Delete("delete t_sys_user_permission where user = #{user} and permission = #{permission}")
    int del(@Param("user") String user, @Param("permission") Integer permission);

    /**
     * 角色权限删除，根据权限删除
     * @param permission
     * @return
     */
    @Delete("delete t_sys_user_permission where permission = #{permission}")
    int delByPermission(Integer permission);

    /**
     * 根据权限id批量删除
     * @param permissions
     * @return
     */
    @Lang(MybatisExtendedLanguageDriver.class)
    @Delete("delete t_sys_user_permission where where permission in (#{permissions})")
    int batchDelByPermissions(@Param("permissions") List<Integer> permissions);

    /**
     * 角色权限删除，根据用户删除
     * @param user
     * @return
     */
    @Delete("delete t_sys_user_permission where user = #{user}")
    int delByUser(String user);

    /**
     * 角色权限删除，根据用户+权限
     * @param user
     * @param permissions
     * @return
     */
    @Lang(MybatisExtendedLanguageDriver.class)
    @Delete("delete t_sys_user_permission where where user = #{user} and permission in (#{permissions})")
    int delByUserPermissions(@Param("user") String user, @Param("permissions") List<Integer> permissions);

    /**
     * 权限读取，根据用户查询
     * @param user
     * @return
     */
    @Select("select sp.* from t_sys_user_permission sup, t_sys_permission sp where sup.permission = sp.id and sup.user = #{user}")
    List<Permission> queryPermissionByUser(String user);
}
